/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds;

import java.awt.Color;
import java.util.Vector;
import net.worlds.net.ChatMessage;


public class Chat {
    private static Vector<ChatMessage> chats = new Vector<ChatMessage>(17);
    public static void addChat(Color c, String m)
    {
        if(chats.size() == chats.capacity())
            chats.remove(chats.get(0));
        chats.add(new ChatMessage(m, c.getRed(), c.getGreen(), c.getBlue()));
    }
    public static void clear()
    {
        chats.clear();
    }
    public static void send()
    {
        if(!Main.chatBarText.equals(""))
        {
            if(Main.getLocalPlayer() != null && Main.getLocalPlayer().getPlayer() != null)
            {
                if(!Main.isClient())
                {
                    String msg = Main.getLocalPlayer().getPlayer().toString()+"> "+Main.chatBarText;
                    addChat(Color.white, msg);
                    Main.getLocalPlayer().getPlayer().getEvent("Chatted").fire(new String[]{Main.chatBarText});
                }
                else
                {
                    final String msg = Main.chatBarText;
                    Runnable r = new Runnable()
                    {
                        public void run() {
                            Main.getClient().sendChat(msg);
                        }
                    };
                    Main.runInMainThread(r);
                }
            }
            Main.chatBarText = "Press . to chat";
            Main.chatBarActive = false;
        }
        else
        {
            Main.chatBarText = "Press . to chat";
            Main.chatBarActive = false;
        }
    }
    public static void removeChat(ChatMessage c)
    {
        chats.remove(c);
    }
    public static ChatMessage[] getChats()
    {
        ChatMessage[] r = new ChatMessage[chats.size()];
        chats.toArray(r);
        return r;
    }
}
